//============================================================================
// Name        : TestC++.cpp
// Author      : GeLiang
// Version     :
// Copyright   : 信号量 互斥锁实现 单向 生产者和消费者
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include <queue>
#include <pthread.h>
#include "common.h"
#include "utils.h"
using namespace std;

#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "Singlton.h"
#include "SyncQueue.h"
pthread_t m_sendThread;
pthread_t m_recvThread;
pthread_mutex_t m_mutex_send;
pthread_mutex_t m_mutex_vector;
pthread_cond_t m_cond_send;
pthread_mutex_t m_mutex_recv;
pthread_cond_t m_cond_recv;
int m_flag_sendThread = 1;
int m_flag_recvThread = 1;
SyncQueue foods;
//int foods = 0;
int eat() {
//	if (foods.empty()) {
//		return -1;
//	}
//	const char* food = foods.front();
//	if (food == NULL) {
//		return -1;
//	}
//	LOGE("eat over");
	if (foods.pop()==NULL) {
		return -1;
	}
	return 0;
//	return foods--;
}
void addfood() {
	foods.push("1");
//				foods++;
}
void* run1(void *data) {
	while (m_flag_sendThread == 1) {
//		pthread_mutex_lock(&m_mutex_send);

		LOGE("=======remove====");
//		LOGE("=======wait====");
		if (m_flag_sendThread == 1) {
//			pthread_cond_wait(&m_cond_send, &m_mutex_send);
			while (eat() != 0) {
//				LOGE("=======eat====");

			}
		} else {
//			LOGE(" exit");
		}

//		pthread_mutex_unlock(&m_mutex_send);
//		LOGE("=======be notify====");
	}
//	LOGE("=======run1 is over====");
	return 0;
}

void* run2(void *data) {
//	while (m_flag_sendThread == 1) {
//		int i = 0;
//	 THREAD_SLEEP(10);
	while (m_flag_sendThread == 1) {
		LOGE("=======add====");
		addfood();
//		pthread_mutex_lock(&m_mutex_send);
//		pthread_cond_signal(&m_cond_send);
//		pthread_mutex_unlock(&m_mutex_send);
//		LOGE("notify pthread_mutex_unlock");
	}

//	}
	LOGE("=======run2 is over====");
	return 0;
}

void testThreadLock() {
	LOGE("VVVVVVVVVVVVVVVVVVV");
	pthread_mutex_init(&m_mutex_send, NULL);
	pthread_cond_init(&m_cond_send, NULL);
	pthread_mutex_init(&m_mutex_vector, NULL);
	pthread_mutex_init(&m_mutex_recv, NULL);
	pthread_cond_init(&m_cond_recv, NULL);
	pthread_create(&m_sendThread, NULL, run1, NULL);
	pthread_create(&m_recvThread, NULL, run2, NULL);
	LOGE("1");
	pthread_mutex_lock(&m_mutex_send);
	m_flag_sendThread = 0;
	LOGE("2");
	pthread_cond_signal(&m_cond_send);
	LOGI("send signal");
	pthread_mutex_unlock(&m_mutex_send);
	pthread_mutex_lock(&m_mutex_send);
	pthread_cond_destroy(&m_cond_send);
	LOGE("3");
	pthread_mutex_unlock(&m_mutex_send);
	m_cond_send = NULL;
	pthread_mutex_destroy(&m_mutex_send);
	pthread_mutex_destroy(&m_mutex_vector);
	m_mutex_send = NULL;
	LOGI("send Thread has destroy");
	pthread_join(m_sendThread, NULL);
	pthread_join(m_recvThread, NULL);
	LOGE("^^^^^^^^^^^^^^^^^^^^");
}
void testArgs(const char* fmt,...){
//	switch (key) {
//			case value:
//
//				break;
//			default:
//				break;
//		}
}
struct SHeader
{
	int 						size;
};
struct SRoom
{
	SHeader 			header;
	int 			room_id;
};
int STATE_INITED=0x01<<0;
int STATE_LOGINED=0x01<<1;
int STATE_ENTRYEDROOM=0x01<<2;
int STATE_REDYED=0x01<<3;
int STATE_STARTED=0x01<<4;
int state = 0;
SHeader *head;
//int main(int argc, char **argv) {
//
//	Singlton::getInstance()->fun();
//	SyncQueue * syncQueue = new SyncQueue();
//	SHeader header;
//	header.size = 1;
//	syncQueue->push(&header);
//	syncQueue->push(&header);
//	SHeader * item =(SHeader *) syncQueue->pop();
//	delete syncQueue;
//
//	item =(SHeader *) syncQueue->pop();
//	if (item!=NULL) {
//	LOGI("item->%d",item->size);
//	}
//
//
//	testThreadLock();
//	long long pl = 4817941867480649728;
//	LOGI("sssn %lld",pl);
//	LOGI("sssn %llx",pl);
//	const char* s  = "f";
//	testArgs("%d,%lld,%f,%f,$s,%p",1,pl,123.00,1.0f,"f",s);
//
//}
